#include "GraphicComponent.h"

#include <cmath>
#include <iostream>

GraphicComponent::~GraphicComponent()
{
    freeGC();
}

void GraphicComponent::init(std::string const & filename)
{
    _img = filename;
    _sprite = _mg.requireSprite();
    _sprite->SetImage(*(_mg.requireImage(_img)));
}

void GraphicComponent::setPos(vector const & pos)
{
    if (_sprite)
        _sprite->SetPosition(pos.x, pos.y);
}

void GraphicComponent::setSubRect(vector const & pos, vector const & size)
{
    if (_sprite)
        _sprite->SetSubRect(sf::IntRect(pos.x,
                                        pos.y,
                                        pos.x + size.x,
                                        pos.y + size.y));
}

void GraphicComponent::freeGC()
{
    _mg.freeImage(_img);
    _mg.freeSprite(_sprite);
}
